//
//  ListenListMoreViewController.m
//  SuperNews
//
//  Created by dllo on 16/1/14.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "ListenListMoreViewController.h"
#import "DetailTableViewCell.h"
#import "ListenTypeModel.h"
#import "RadioModel.h"
#import "PlayerViewController.h"

@interface ListenListMoreViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableV;
@property (nonatomic, strong) NSMutableArray *listArr;
@property (nonatomic, assign) NSInteger number;

@end

@implementation ListenListMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.number = 0;
    self.listArr = [NSMutableArray array];
    [self createViews];
    [self getData];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    for (UIView *view in self.navigationController.navigationBar.subviews) {
        if ([view isKindOfClass:[UISegmentedControl class]]) {
            view.hidden = YES;
        }
    }
    self.navigationController.navigationBarHidden = NO;
}

- (void)createViews
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.cname;
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:18.0], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, nil];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bobo_back"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    
    [self.tableV registerClass:[DetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

- (void)leftAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListenTypeModel *typeModel = [self.listArr objectAtIndex:indexPath.row];
    RadioModel *radioModel = typeModel.radio;
    PlayerViewController *playerVC = [PlayerViewController sharePlayerViewController];
    playerVC.cid = typeModel.tid;
    playerVC.docid = radioModel.docid;
    [self.navigationController pushViewController:playerVC animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListenTypeModel *typeModel = [self.listArr objectAtIndex:indexPath.row];
    RadioModel *radioModel = typeModel.radio;
    DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.imageV sd_setImageWithURL:(NSURL *)radioModel.imgsrc placeholderImage:[UIImage imageNamed:@"placehodler"]];
    cell.titleLabel.text = radioModel.tname;
    cell.detailLabel.text = radioModel.title;
    return cell;
}

- (void)getData
{
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/topicset/ios/radio/%@/%ld-20.html", self.cid, self.number];
    [AFNetTool getNetWithURL:str body:nil headFile:nil responseStyle:YZQJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableArray *tArr = [responseObject objectForKey:@"tList"];
        NSMutableArray *radioArr = [NSMutableArray array];
        for (NSDictionary *tmpDic in tArr) {
            ListenTypeModel *typeModel = [[ListenTypeModel alloc] init];
            NSDictionary *radioDic = [tmpDic objectForKey:@"radio"];
            RadioModel *radioModel = [[RadioModel alloc] init];
            [radioModel setValuesForKeysWithDictionary:radioDic];
            typeModel.radio = radioModel;
            typeModel.tid = [tmpDic objectForKey:@"tid"];
            [radioArr addObject:typeModel];
        }
        [self.listArr addObjectsFromArray:radioArr];
        [self.tableV reloadData];
        self.number += 20;
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
